Skip to content

Add random method to Unitary.#707

Merged
Aubaert merged 3 commits intoQuandela:release/1.2.0from
anthonyrtw:unitary-random-matrix
Jan 21, 2026
Merged

Add random method to Unitary.#707
Aubaert merged 3 commits intoQuandela:release/1.2.0from
anthonyrtw:unitary-random-matrix

Conversation

@anthonyrtw
Copy link
Contributor

To generate random unitaries in Perceval, one has to do the following:

from perceval import Unitary, Matrix

m = 10 # Number of modes
random_unitary = Unitary(Matrix.random_unitary(m))

Matrix.random_unitary is almost always used to create a random Unitary object. So, for conciseness I have added a random static method to Unitary that calls Matrix.random_unitary.

from perceval import Unitary

m = 10
random_unitary = Unitary.random(m)

return f"Unitary({', '.join(params)})"

@staticmethod
def random(m: int) -> "Unitary":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid setting the returned value in quotes by adding from __future__ import annotations at the beginning of this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Aubaert.
My first commit did this, but it still failed the auto-test.
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think it's because we removed this from __future__ import annotations in the release branch. In that case, can you add this comment after this line?

# Python 3.11 : Replace using Self typing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done there.

@Aubaert Aubaert merged commit 32d28ed into Quandela:release/1.2.0 Jan 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants